#
# Rules to add PyMite flight plan interpreter
#

# Paths
PYMITE		:= $(FLIGHTLIB)/PyMite
PYMITELIB	:= $(PYMITE)/lib
PYMITEPLAT	:= $(PYMITE)/platform/openpilot
PYMITETOOLS	:= $(PYMITE)/tools
PYMITEVM	:= $(PYMITE)/vm
PYMITEINC	:= $(PYMITEVM)
PYMITEINC	+= $(PYMITEPLAT)
PYMITEINC	+= $(OUTDIR)

# Flight plans
FLIGHTPLANLIB	?= $(OPMODULEDIR)/FlightPlan/lib
FLIGHTPLANS	?= $(OPMODULEDIR)/FlightPlan/flightplans

# Extra modules
PYMODULES	?= FlightPlan

# Modules
PYSRC		+= $(foreach mod, $(PYMODULES), $(wildcard $(OPMODULEDIR)/$(mod)/*.c))

# PyMite virtual machine and platform files
PYSRC		+= $(wildcard $(PYMITEVM)/*.c)
PYSRC		+= $(wildcard $(PYMITEPLAT)/*.c)

# Autogenerated files
PYLIB		+= $(addprefix $(OUTDIR)/, pmlib_img.c pmlib_nat.c)
PYLIB		+= $(addprefix $(OUTDIR)/, pmlibusr_img.c pmlibusr_nat.c)

# Scripts
PYSCRIPTS	+= $(wildcard $(PYMITELIB)/*.py)
PYSCRIPTS	+= $(wildcard $(PYMITEPLAT)/*.py)
PYSCRIPTS	+= $(wildcard $(FLIGHTPLANLIB)/*.py)
PYSCRIPTS	+= $(wildcard $(FLIGHTPLANS)/*.py)

# Generate code for PyMite
$(PYSRC): | $(PYLIB) $(OUTDIR)/pmfeatures.h

$(OUTDIR)/pmfeatures.h: $(PYSCRIPTS)
	@$(ECHO) $(MSG_PYMITEINIT) $(call toprel, $@)
	$(V1) $(PYTHON) $(PYMITETOOLS)/pmGenPmFeatures.py $(PYMITEPLAT)/pmfeatures.py > $@

$(OUTDIR)/pmlib_img.c: | $(OUTDIR)/pmlib_nat.c

$(OUTDIR)/pmlib_nat.c: $(PYSCRIPTS)
	@$(ECHO) $(MSG_PYMITEINIT) $(call toprel, $@)
	$(V1) $(PYTHON) $(PYMITETOOLS)/pmImgCreator.py -c -s --memspace=flash \
			-f $(PYMITEPLAT)/pmfeatures.py \
			-o $(OUTDIR)/pmlib_img.c \
			--native-file=$(OUTDIR)/pmlib_nat.c \
			$(PYMITELIB)/list.py \
			$(PYMITELIB)/dict.py \
			$(PYMITELIB)/__bi.py \
			$(PYMITELIB)/sys.py \
			$(PYMITELIB)/string.py \
			$(wildcard $(FLIGHTPLANLIB)/*.py)

$(OUTDIR)/pmlibusr_img.c: | $(OUTDIR)/pmlibusr_nat.c

$(OUTDIR)/pmlibusr_nat.c: $(PYSCRIPTS)
	@$(ECHO) $(MSG_PYMITEINIT) $(call toprel, $@)
	$(V1) $(PYTHON) $(PYMITETOOLS)/pmImgCreator.py -c -u \
			-f $(PYMITEPLAT)/pmfeatures.py \
			-o $(OUTDIR)/pmlibusr_img.c \
			--native-file=$(OUTDIR)/pmlibusr_nat.c \
			$(FLIGHTPLANS)/test.py

# Add to the source and include lists
SRC		+= $(PYSRC)
SRC		+= $(PYLIB)
EXTRAINCDIRS	+= $(PYMITEINC)
EXTRAINCDIRS	+= $(foreach mod, $(PYMODULES), $(OPMODULEDIR)/$(mod)/inc)
